<?php 
require_once 'init.php';
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
//操作
if(!empty($_GET['id'])){
	$act = "edit";
	$id = $_GET['id'];
}
//提交处理
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	$userName = $_POST['userName'];
	$newPwd   = $_POST['newPwd'];
	$againPwd = $_POST['againPwd'];
	$nickName = $_POST['nickName'];
	$type = $_POST['type'];
	$act      = $_POST['act'];
	$shortName= $_POST['shortName'];
	
	if($act == "edit"){
		if(empty($_POST['id'])){
			//程序出错，请刷新后再次操作
			$error = 3;
		}else{
			$id=$_POST['id'];
			if($newPwd==$againPwd){
				$db->sql="UPDATE admin SET userName='".$userName."',nickName='".$nickName."',userpwd='".sha1($newPwd)."',type=".$type.",updateDate=".time().",shortName='".$shortName."' WHERE id = ".$id;
				$db->doExecute();
				$success = 1;
			}else{
				//错误2：密码不一致
				$error = 2;
			}
		}	
	}else{
		$db->sql = "SELECT id FROM `admin` WHERE userName = '".$userName."'";
		$id1 = $db->doResult2();
		if ( empty($id1) ){
			if($newPwd == $againPwd){
				$db->sql = "INSERT INTO admin(userName,nickName,userpwd,createDate,type,updateDate,shortName) VALUES('".$userName."','".$nickName."','".sha1($newPwd)."',".time().",".$type.",".time().",'".$shortName."')";
				$db->doExecute();
				$success = 1;
			}else{
				//错误2：密码不一致
				$error = 2;
			}	
		}else {
			//错误1：用户已经存在
			$error = 1;
		}
	}
}
if(!empty($id)){
	$db->sql = "SELECT * FROM admin WHERE id = ".$id;
	$userInfo = $db->doResult2();
	//var_dump($userInfo);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />
<style type="">
	span{width: 85px;font-weight: bold;display: block;float: left;}
</style>
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {
	$("form").submit(function(){
		if( !$("input[name='userName']").val() || !$("input[name='newPwd']").val() || !$("input[name='againPwd']").val() || !$("input[name='nickName']").val() ){
			alert("都为必填项，不能为空！");
			return false;
		}
		else if($("select[name='type']").val().replace(/(^\s*)|(\s*$)/g,"").length == 0){
			alert("用户类别未选择！");
			return false;
		}
	});
});
</script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>

</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		
		<div id="main-content">
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">用户<?php if($act=="edit") echo "修改";else echo "添加";?></p>

			<div class="clear"></div>
			<!-- End .clear -->

			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-header">

					<h3>用户<?php if($act=="edit") echo "修改";else echo "添加";?></h3>

					<div class="clear"></div>

				</div>
				<!-- End .content-box-header -->

				<div class="content-box-content">

				<!-- Start Notifications -->
				<?php if($success==1):?>
				<div class="notification success png_bg">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
						<?php if($id) echo "修改";else echo "添加";?>用户成功！
					</div>
				</div>
				<?php endif;?>
				<?php if($error==1 || $error==2 || $error==3):?>
				<div class="notification error png_bg">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
					<?php if($error == 1) echo "用户已经存在";elseif($error==2) echo "密码不一致！";elseif($error==3) echo "出错，请刷新后重新操作！";?>
					</div>
				</div>
				<?php endif;?>
				<!-- End Notifications -->
					<div class="tab-content default-tab" id="tab1">
						<form action="#" method="post">
							<fieldset>
								<!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
								<p>
									<span>帐号:</span> 
									<input class="text-input small-input" type="text" id="oldPwd" name="userName" value="<?php echo $userInfo['userName'];?>" /> 
								</p>
								<p>
									<span>新密码:</span> 
									<input class="text-input small-input" type="password" id="newPwd" name="newPwd" value="" /> 
								</p>
								<p>
									<span>重复密码:</span> 
									<input class="text-input small-input" type="password" id="againPwd" name="againPwd" value="" /> 
								</p>
								<p>
									<span>名称:</span> 
									<input class="text-input small-input" type="text" id="againPwd" name="nickName" value="<?php echo $userInfo['nickName'];?>" /> 
								</p>
								<p>
									<span>昵称:</span> 
									<input class="text-input small-input" type="text" id="shortName" name="shortName" value="<?php echo $userInfo['shortName'];?>" /> 
								</p>
								<p>
									<span>用户类别:</span> 
									<select name="type">
									  <option value="">请选择</option>
									  <option value="2" <?php echo ($userInfo['type']==2) ? "selected":"";?>>选品</option>
									  <option value="0" <?php if(isset($userInfo['type']) && $userInfo['type']==0) echo "selected";?>>其他</option>
									</select>
								</p>
								<p>
									<input type="hidden" name="id" value="<?php echo $id;?>"/>
									<input type="hidden" name="act" value="<?php echo $act;?>"/>
									<input class="button" type="submit" value="<?php if($act=="edit") echo "修改";else echo "添加";?>" />
									<input class="button" type="reset" value="清空" />
								</p>

							</fieldset>

							<div class="clear"></div>
							<!-- End .clear -->

						</form>

					</div>
					<!-- End #tab2 -->

				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
